package com.eCommerce.server.svco.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.eCommerce.corecontract.criteria.UserCriteria;
import com.eCommerce.corecontract.dto.Role;
import com.eCommerce.corecontract.dto.UserDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommerceSVCOException;

import com.eCommerce.server.corebackend.sisv.IUserSISV;
import com.eCommerce.server.corebackend.sisv.SISVFinder;
import com.eCommerce.server.corebackend.util.BackEndConstants;

import com.eCommercecorecontract.svco.IUserSVCO;

@Transactional
public class UserSVCOImpl extends AbstractCommonSVCO implements IUserSVCO , Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger log =Logger.getLogger(UserSVCOImpl.class);
	private SISVFinder sisvFinder = null;

	private IUserSISV _userSISV=null;
	public UserSVCOImpl(SISVFinder sisvFinder) throws Exception{
		this.sisvFinder = sisvFinder;
		_userSISV = (IUserSISV) sisvFinder.get(BackEndConstants.SISV_USER );
	
		
	}
	
	public Long creerUser(UserDTO userDTO) throws EcommerceException {
		// TODO Auto-generated method stub
		
		try
		{
			if (_userSISV.validerUser(userDTO, "CREATE")) _userSISV.creerUser(userDTO); 
			else
			{
				Object[] arg1 = new Object[1];
				arg1[0]=userDTO.getNom();
				EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOUSER-005", arg1);
				log.error(svcoException.getMessage());
				throw svcoException;
			}
		}
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception ex)
		{
			Object[] arg1 = new Object[1];
			arg1[0]=userDTO.getNom();
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOUSER-001", arg1, ex.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
		
		
		
		return null;
	}
	public List listerUsers()
			throws EcommerceException {
		// TODO Auto-generated method stub
		try 
		{
			return _userSISV.listerUsers();//(userCriteria);
		}
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception ex )
		{
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOUSER-004", arg1, ex.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
		
		
	}
	public void modifierUser(UserDTO userDTO) throws EcommerceException {
		try 
		{
			 _userSISV.modifierUser(userDTO);//rechercherUsers(userCriteria);
		}
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception ex )
		{
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOUSER-002", arg1, ex.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
		
	}
	public List rechercherUsers(UserCriteria userCriteria)
			throws EcommerceException {
		try 
		{
			return _userSISV.rechercherUsers(userCriteria);
		}
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception ex )
		{
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOUSER-004", arg1, ex.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
	}
	public UserDTO seConnecter(UserDTO userDTO) throws EcommerceException {
		// TODO Auto-generated method stub
		UserDTO userConnected=null;
		try 
		{
			userConnected= _userSISV.seConnecter(userDTO);//(userCriteria);
			return userConnected;
		}
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception ex )
		{
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOUSER-006", arg1, ex.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
	}
	public void supprimerUsers(Set<UserDTO> setOfUserDTO, UserDTO userConnected)
			throws EcommerceException {
		try 
		{ 
			//return _userSISV.supprimer(userDTO))(userDTO);//(userCriteria);
			for(UserDTO userDTO: setOfUserDTO)    
				{
				if (userConnected.getRole().equals(Role.SUPERADMIN.toString())==false)
				{
					Object[] arg1 = new Object[1];
					
					EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOUSER-008",arg1);
					log.error(svcoException.getMessage());
					throw svcoException;
				}
					_userSISV.supprimer(userDTO);
				}
		}
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception ex )
		{
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOUSER-005", arg1, ex.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
		
	}
	public UserDTO visualiserUser(UserDTO userDTO) throws EcommerceException {
		// TODO Auto-generated method stub
		try 
		{
			return _userSISV.visualiserUser(userDTO);//(userCriteria);
		}
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception ex )
		{
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOUSER-003", arg1, ex.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
	}

}
